﻿@{
    ViewBag.Title = "Index";
}

<!-- Main content -->
<section class="content" ng-app="app" ng-controller="dataController">
    <div>
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @*<a class="navbar-brand" href="#"></a>*@
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav"></ul>
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <button class="btn btn-default" data-toggle="modal" data-target="#editOutModal" ng-click="add(0)">+支出</button>
                            <button class="btn btn-default" data-toggle="modal" data-target="#editInModal" ng-click="add(1)">+收入</button>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search" ng-model="searchName">
                        </div>
                        <button ng-click="list()" class="btn btn-default">查询</button>
                    </form>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <!-- /.box-header -->
                    <div class="box-body">
                        <table class="table table-hover">
                            <tr>
                                <th style="width: 10px">#</th>
                                <th>日期</th>
                                <th>账户</th>
                                <th>金额</th>
                                <th>类别</th>
                                <th>操作人</th>
                                @*<th></th>*@
                            </tr>
                            <tr ng-repeat="(index,data) in dataList">
                                <td>{{pageSize*(pageIndex-1)+index+1}}.</td>
                                <td>{{data.createTime|date:'yyyy-MM-dd HH:mm'}}</td>
                                <td>{{data.bankName}}({{data.cardNo|bankCardNumberFormat}})</td>
                                <td>
                                    <span class="badge bg-red" ng-show="data.isOut==0">{{data.amount|currency:"￥"}}</span>
                                    <span class="badge bg-green" ng-show="data.isOut==1">{{data.amount|currency:"￥"}}</span>
                                </td>
                                <td>{{data.typeName}}</td>
                                <td>{{data.createUserName}}({{data.email}})</td>
                                @*<td>
                                    <button class="btn btn-default btn-xs" ng-click="edit(data.id)" data-toggle="modal" data-target="#editModal">编辑</button>
                                    <button class="btn btn-default btn-xs" ng-click="delete(data.id)">删除</button>
                                </td>*@
                            </tr>
                        </table>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer clearfix">
                        <span>{{$scope.total}}</span>
                        <ul class="pagination pagination-sm no-margin pull-right">
                            <li><a href="#">&laquo;</a></li>
                            <li ng-repeat="i in pageRange " class="{{i==pageIndex?'active':''}}"><a href="#">{{i}}</a></li>
                            <li><a href="#">&raquo;</a></li>
                        </ul>
                    </div>

                    <div class="overlay" ng-show="loading">
                        <i class="fa fa-refresh fa-spin"></i>
                    </div>
                </div>
                <!-- /.box -->
            </div>
        </div>
        <div class="modal" id="editOutModal" role="dialog" aria-labelledby="editOutModalTitle">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="editOutModalTitle">支出</h4>
                    </div>
                    <div class="modal-body">
                        <form name="editOutForm" class="form-horizontal" ng-submit="editOutForm.$valid&&save()" novalidate="novalidate">
                            <div class="box-body">
                                <div class="form-group {{editOutForm.bankCardId.$dirty?(editOutForm.bankCardId.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">账户</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="bankCardId" ng-model="editData.bankCardId" required>
                                            <option value="">请选择</option>
                                            <option ng-repeat="data in bankCards" value="{{data.id}}">{{data.bankName}}({{data.cardNo}})</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group {{editOutForm.typeId.$dirty?(editOutForm.typeId.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">类别</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="typeId" required ng-model="editData.typeId">
                                            <option value="">请选择</option>
                                            <option ng-repeat="type in outTypes" value="{{type.id}}">{{type.name}}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group {{editOutForm.amount.$dirty?(editOutForm.amount.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">金额</label>
                                    <div class="col-sm-10">
                                        <input type="number" name="amount" class="form-control" ng-model="editData.amount" ng-pattern="/^\d+(\.\d+)?$/" required />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注</label>
                                    <div class="col-sm-10">
                                        <textarea class="form-control" ng-model="editData.memo"></textarea>
                                    </div>
                                </div>
                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer">
                                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关 闭</button>
                                <button type="submit" class="btn btn-primary pull-right" ng-disabled="editOutForm.$invalid||saving">保 存</button>
                            </div>
                        </form>
                    </div>

                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
        <div class="modal" id="editInModal" role="dialog" aria-labelledby="editInModalTitle">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="editInModalTitle">收入</h4>
                    </div>
                    <div class="modal-body">
                        <form name="editInForm" class="form-horizontal" ng-submit="editInForm.$valid&&save()" novalidate="novalidate">
                            <div class="box-body">
                                @*  <div class="form-group {{editInForm.name.$dirty?(editInForm.name.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">名称</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name="name" placeholder="名称" ng-model="editData.name" required>
                                    </div>
                                </div>*@
                                <div class="form-group  {{editInForm.bankCardId.$dirty?(editInForm.bankCardId.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">账户</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="bankCardId" ng-model="editData.bankCardId" required>
                                            <option value="">请选择</option>
                                            <option ng-repeat="data in bankCards" value="{{data.id}}">{{data.bankName}}({{data.cardNo}})</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group {{editInForm.typeId.$dirty?(editInForm.typeId.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">类别</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="typeId" ng-model="editData.typeId" required>
                                            <option value="">请选择</option>
                                            <option ng-repeat="type in inTypes" value="{{type.id}}">{{type.name}}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group {{editInForm.amount.$dirty?(editInForm.amount.$invalid?'has-error':'has-success'):''}}">
                                    <label class="col-sm-2 control-label">金额</label>
                                    <div class="col-sm-10">
                                        <input type="number" name="amount" class="form-control" ng-model="editData.amount" ng-pattern="/^\d+(\.\d+)?$/" required />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注</label>
                                    <div class="col-sm-10">
                                        <textarea class="form-control" ng-model="editData.memo"></textarea>
                                    </div>
                                </div>
                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer">
                                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关 闭</button>
                                <button type="submit" class="btn btn-primary pull-right" ng-disabled="editInForm.$invalid||saving">保 存</button>
                            </div>
                        </form>
                    </div>

                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
    </div>
</section>


@section scripts{

    <script src="//cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
    <script type="text/javascript">
        angular.module("app.service", [])
            .factory("actionUrl", function () {

                return {
                    saveUrl: "@Url.Action("Save")",
                    listUrl: "@Url.Action("List")",
                    deleteUrl: "@Url.Action("Delete")",
                    listTypeUtl: "@Url.Action("ListType")",
                    listBankCardUrl: "@Url.Action("ListBankCard")"
                };

            })
            .filter("bankCardNumberFormat", function () {

                return function (input, len) {
                    len = len || 4;
                    if (input) {
                        var ary = [];
                        for (var i = 0; i < input.length; i += len) {
                            ary.push(input.substr(i, len));
                        }
                        return ary.join(" ");
                    }
                    return input;
                };
            })

    </script>
    <script type="text/javascript">
        angular.module("app", ["app.service"])
            .controller("dataController", function ($scope, $http, actionUrl) {
                //debugger;
                var _isOut = 1;
                $scope.editData = {
                    id: 0,
                    name: "",
                    amount: 0,
                    typeId: "",
                    bankCardId: "",
                    remarks: "",
                    reset: function () {
                        this.id = 0;
                        this.amount = 0;
                        this.name = "";
                        this.typeId = "";
                        this.bankCardId = "";
                        this.remarks = "";
                    },
                    set: function (obj) {
                        this.id = obj.id;
                        this.amount = obj.amount;
                        this.name = obj.name;
                        this.typeId = obj.typeId.toString();
                        this.bankCardId = obj.bankCardId.toString();
                        this.remarks = obj.remarks;
                    },
                    get: function () {
                        return {
                            id: this.id,
                            amount: this.amount,
                            name: this.name,
                            typeId: this.typeId,
                            bankCardId: this.bankCardId,
                            remarks: this.remarks
                        };
                    }
                };

                $scope.add = function () {
                    $scope.editData.reset();
                };

                $scope.edit = function (id) {
                    $scope.editData.reset();
                    var data = _.find($scope.dataList, function (c) { return c.id == id; });
                    if (data) {
                        $scope.editData.id = data.id;
                        $scope.editData.amount = data.amount;
                        $scope.editData.name = data.name;
                        $scope.editData.typeId = data.typeId.toString();
                        $scope.editData.bankCardId = data.bankCardId.toString();
                    }
                };

                $scope.delete = function (id) {
                    if (confirm("删除后无法恢复，确定要删除吗？")) {
                        $http.post(actionUrl.deleteUrl, { id: id })
                            .success(function (r) {
                                if (r.success) {
                                    $scope.list();
                                }
                            })
                            .error(function () {
                            });
                    }
                };

                $scope.save = function () {
                    $scope.saving = true;
                    $http.post(actionUrl.saveUrl, $scope.editData.get()).success(function (r) {
                        if (r.success) {
                            $scope.editData.reset();
                            $scope.list();
                            $(".modal").modal('hide');
                        }
                        $scope.saving = false;
                    }).error(function () {
                        $scope.saving = false;
                    });

                };
                $scope.pageIndex = 1;
                $scope.pageSize = 20;
                $scope.showPageCount = 5; //要显示的页数
                $scope.pageRange = [];

                function getPageRange() {
                    var totalPageCount = ($scope.total + $scope.pageSize - 1) / $scope.pageSize; // 总页数
                    var start = $scope.pageIndex;
                    ;
                    // 判断当前页码位置 前(页码小于显示页数) 后（页码大于总页数-显示页数）
                    if ($scope.pageIndex <= $scope.showPageCount) {
                        start = 1;
                    } else if ($scope.pageIndex >= totalPageCount - $scope.showPageCount) {
                        start = totalPageCount - $scope.showPageCount;
                    }
                    var ary = [];
                    for (var i = start; i <= $scope.showPageCount && i <= totalPageCount; i++) {
                        ary.push(i);
                    }
                    return ary;
                }

                $scope.loading = true;
                $scope.dataList = [];
                $scope.list = function () {
                    $scope.loading = true;
                    $http.post(actionUrl.listUrl, { name: $scope.searchName, pageIndex: $scope.pageIndex, pageSize: $scope.pageSize })
                        .success(function (r) {
                            $scope.dataList = r.data;
                            $scope.total = r.total;
                            $scope.pageRange = getPageRange();
                            debugger;
                            $scope.loading = false;
                        })
                        .error(function () {
                            $scope.loading = false;
                        });
                };

                $scope.list();

                // 获取类别
                $scope.inTypes = [];
                $scope.outTypes = [];
                $http.post(actionUrl.listTypeUtl).success(function (r) {
                    if (r.success) {
                        _.each(r.data, function (r) { r.id = r.id.toString(); });
                        $scope.inTypes = _.where(r.data, { isOut: 1 });
                        $scope.outTypes = _.where(r.data, { isOut: 0 });


                    }
                }).error(function () { });

                $scope.bankCards = [];
                $http.post(actionUrl.listBankCardUrl).success(function (r) {
                    if (r.success) {
                        _.each(r.data, function (r) { r.id = r.id.toString(); });

                        $scope.bankCards = r.data;
                    }
                }).error(function () { });
            });
    </script>
}